<template>
  <div class="sub-comment">
    <div class="avatar-username">
      <el-avatar :size="30" :src="subComment.avatar" />
      <span class="name">{{ subComment.username }}</span>
    </div>
    <div class="content">
      {{ subComment.content }}
    </div>
    <div class="day-liked">
      <span class="day">{{ dateFormat(subComment.day) }}</span>
      <div class="liked-comment">
        <div class="post-likes">
          <button class="like-button" @click="thumbsUp" :class="{ liked: isLiked }">
            <font-awesome-icon :icon="['far', 'heart']" v-if="!isLiked" />
            <font-awesome-icon :icon="['fas', 'heart']" v-else />
          </button>
          <span class="count">{{ liked }}</span>
        </div>
        <el-popconfirm title="确定要删除这条评论吗?" @confirm="deleteReplies">
          <template #reference>
            <div class="post-delete" v-if="userId === subComment.userId">
              <el-icon size="18" style="margin: -1px 6px 0 6px; cursor: pointer">
                <MoreFilled />
              </el-icon>
            </div>
          </template>
        </el-popconfirm>
      </div>
    </div>
    <el-divider style="margin-top: -1px" />
  </div>
</template>

<script setup>
import { useUserStore } from '@/stores'
import { dateFormat } from '@/utils/strutils'
import { deleteReply, isUserLikeReply, thumbsUpReply } from '@/api/forum'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { defineEmits, defineProps, ref } from 'vue'

const userStore = useUserStore()

const userId = userStore.getUser()

const props = defineProps({
  obj: Object,
})

const emit = defineEmits(['deleteReply'])

const subComment = props.obj

// 喜爱数量
const liked = ref(subComment.liked)

// 点赞
const thumbsUp = async () => {
  const { data } = (await thumbsUpReply(subComment.id)).data
  liked.value = data
  isLiked.value = !isLiked.value
}

// 当前用户是否喜欢过该评论
const isLiked = ref(false)

// 判断当前用户是否喜欢
const isUserLike = async () => {
  const { data } = (await isUserLikeReply(subComment.id)).data
  isLiked.value = data
}

isUserLike()

// 删除回复
const deleteReplies = async () => {
  const res = (await deleteReply(subComment.id)).data
  if (res.success) {
    emit('deleteReply')
  } else {
    ElNotification({
      title: '错误',
      message: '删除回复失败，请稍后重试',
      type: 'error',
    })
  }
}
</script>

<style scoped less="scss">
.sub-comment {
  width: 90%;
  min-height: 50px;

  .avatar-username {
    display: flex;
    justify-content: start;
    align-items: center;
    gap: 5px;
  }

  .content {
    padding-left: 20px;
  }

  .day-liked {
    display: flex;
    justify-content: space-between;

    padding: 10px 10px 5px 10px;

    .day {
      color: grey;
    }

    .liked-comment {
      display: flex;
      gap: 10px;

      .post-likes,
      .post-comments {
        display: flex;
        align-items: center;
        justify-content: start;
      }

      .post-delete {
        display: flex;
        align-items: center;
        justify-content: start;
      }

      .count {
        cursor: pointer;
      }

      .like-button {
        width: 28px;
        height: 22px;
        background: none;
        border: none;
        cursor: pointer;
        font-size: 1.2em;
        color: #333;
        margin-right: 3px;
      }
    }
  }

  .like-button.liked {
    color: red !important;
  }
}
</style>
